<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

require_once ('HTMLElement.php');

/**
 * 
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 *
 */
class FormHelper extends Helper
{

  public function hidden($name, $value = null, $attribs = null, $params = null)
  {
    $e = new HTMLElement('input', $attribs, $params);
    
    $e->attr('type', 'hidden');
    $e->attr('name', $name);
    $e->attr('value', $value);
    
    return $e;
  }

  /**
   *
   * @param $data
   * @param $selectedKey
   * @param $attribs
   * @param $params
   * @return unknown_type
   */
  public function options($data, $selectedKey = null, $attribs = null, $params = null)
  {
    $output = '';
    
    $params = Dictionary::parseFrom($params);
    
    foreach ($data as $value => $label) {
      $_attribs = new Dictionary($attribs);
      $_attribs->set('value', $value);
      
      if ($value == $selectedKey) {
        $_attribs->set('selected', 'true');
      }
      
      $option = new HTMLElement('option', $_attribs, $params, $label);
      $output .= $option;
      $output .= $params->get('itemSeparator');
    }
    
    return $this->output($output);
  }

  /**
   *
   * @param $data
   * @param $selectedKey
   * @param $attribs
   * @param $params
   * @return unknown_type
   */
  public function checkboxes($data, $checkedKeys = null, $attribs = null, $params = null)
  {
    if (is_null($checkedKeys))
      $checkedKeys = array();
    
    $params = Dictionary::parseFrom($params);
    
    $output = '';
    
    foreach ($data as $value => $label) {
      $_attribs = new Dictionary($attribs);
      $_attribs->set('type', 'checkbox');
      $_attribs->set('value', $value);
      
      if (in_array($value, $checkedKeys)) {
        $_attribs->set('checked', 'true');
      }
      
      $option = new HTMLElement('input', $_attribs, $params);
      $output .= $option;
      $output .= ' ' . $label;
      $output .= $params->get('itemSeparator');
    }
    
    return $this->output($output);
  }

  /**
   *
   * @param $data
   * @param $selectedKey
   * @param $attribs
   * @param $params
   * @return unknown_type
   */
  public function radios($data, $selectedKey = null, $attribs = null, $params = null)
  {
    $params = Dictionary::parseFrom($params);
    
    $output = '';
    
    foreach ($data as $value => $label) {
      $_attribs = new Dictionary($attribs);
      $_attribs->set('type', 'radio');
      $_attribs->set('value', $value);
      
      if ($value == $selectedKey) {
        $_attribs->set('checked', 'true');
      }
      
      $option = new HTMLElement('input', $_attribs, $params);
      $output .= $option;
      $output .= ' ' . $label;
      $output .= $params->get('itemSeparator');
    }
    
    return $this->output($output);
  }

}

?>